﻿using System;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;

namespace CustomizeControl
{
    public class Light : Panel
    {
        public Color LightColor { get; set; } = Constants.灰色;

        public Light()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.UserPaint |
                 ControlStyles.ResizeRedraw |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.SupportsTransparentBackColor, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            int num = Math.Min(base.Width, base.Height);
            SolidBrush solidBrush = new SolidBrush(LightColor);
            RectangleF rectangleF = new RectangleF(0, 0, (float)(num), (float)(num));
            e.Graphics.FillEllipse(solidBrush, rectangleF);
        }

        // 中心颜色与边缘颜色属性
        //public Color CenterColor { get; set; } = Color.White;
        //public Color EdgeColor { get; set; } = Color.Red;
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);

        //    // 创建圆形路径
        //    using (GraphicsPath path = new GraphicsPath())
        //    {
        //        path.AddEllipse(ClientRectangle); // 以控件客户区范围绘制椭圆
        //        this.Region = new Region(path);    // 设置区域为圆形，限制交互范围

        //        // 创建径向渐变画刷
        //        using (PathGradientBrush brush = new PathGradientBrush(path))
        //        {
        //            brush.CenterPoint = new PointF(Width / 2f, Height / 2f); // 中心点
        //            brush.CenterColor = CenterColor;
        //            brush.FocusScales = new PointF(0.005f, 0.005f);
        //            brush.SurroundColors = new Color[] { EdgeColor };       // 边缘颜色

        //            // 抗锯齿优化
        //            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        //            e.Graphics.FillPath(brush, path); // 填充圆形渐变
        //        }

        //        using (var pen = new Pen(EdgeColor, 1))
        //        {
        //            e.Graphics.DrawPath(pen, path);
        //        }
        //    }
        //}

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate(); // 强制重绘以更新圆形和渐变
        }
    }
}
